<?php
Zend_Loader::loadClass('Application_Form');
Zend_Loader::loadClass('Application_Form_Element_GasControls');

class Application_Form_Gas extends Application_Form
{
	protected $_formDecorators = array(
			'FormElements',
			'Form',
			array('HtmlTag', array('tag' => 'div', 'id' => 'form-wrap')),
	);

	protected $_displaygroupDecorators = array(
			'FormElements',
			'Fieldset',
	);

	protected $_elementDecorators = array(
			'viewHelper',
			array(array('htmltag1' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-span')),
			array('Label', array('tag' => 'div', 'tagClass' => 'form-span'))
	);

	public function __construct() {
		parent::__construct();
		$this->addJavascript("/validator/jquery.validationEngine.js");
		$this->addJavascript("/validator/languages/jquery.validationEngine-en.js");
		$this->addStylesheet("/validator/validationEngine.jquery.css");
		$this->addJavascript("/dataTables/jquery.dataTables.min.js");
		$this->addStylesheet("/dataTables/jquery.dataTables.css");
		$this->addStylesheet("/dataTables/demo_table_jui.css");
		$this->addStylesheet("/dataTables/fix.css");
	}

	public function run() {

		$request = Zend_Controller_Front::getInstance()->getRequest();
		if (!$request->isXmlHttpRequest()) {
			// Contesto non-AJAX
			$this->_initForm();
			$this->_view->browserForm = $this->_form->getSubForm("browser")->render($this->_view);
			$this->_view->detailForm = $this->_form->getSubForm("detail")->render($this->_view);
			return;

		} else {
			// Contesto AJAX
			$layout = Zend_Layout::getMvcInstance();
			$layout->disableLayout();

			$ret = "";
			if ($request->isPost()) {
				// POST data
				foreach ($request->getParams() as $paramName => $paramValue) {
					$urlParams[$paramName] = urlencode($paramValue);
				}
				$this->_initForm();
				if ($this->_form->getSubForm("detail")->isValid($request->getParams())) {
					$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
					$redirector->setGotoRoute($urlParams, 'ajax');
					$redirector->redirectAndExit();
				} else {
					$messages = $this->_form->getSubForm("detail")->getMessages();
					if (array_key_exists('gas_name', $messages)) {
						array_unshift($messages['gas_name'], "Corregere gli errori segnalati");
					} else {
						$messages['gas_name'] = array ("generic" => "Corregere gli errori segnalati");
					}
					$ret = array("esito" => -1, "id" => $request->getParam('id'), "messages" => $messages);
				}
			} else {
				// Servizi ausiliari form
				switch ($request->getParam('function')) {
					default:
						$ret = null;
				}
			}
			echo Zend_Json::encode($ret);
		}

	}

	protected function _initForm()
	{
		$browserForm = new Zend_Form(array("id" => "browser"));
		$detailForm = new Zend_Form(array("id" => "detail"));
		$this->_form->addSubForm($browserForm, "browser");
		$this->_form->addSubForm($detailForm, "detail");

		///// TOP (dataTables) /////
		
		Zend_Loader::loadClass('Gases');
		$gas = new Gases();
		$recordSet = $gas->select()->from($gas, array("id", "name"))
		->where("status > ?", 0)
		->order("name");
		$this->_view->gases = $gas->fetchAll($recordSet)->toArray();
		
		///// BOTTOM (zend form) /////
		
		$campo1 = new Zend_Form_Element_Hidden('gas_order');
		$campo1->setValue(10); //($value)  setOptions(array("value" => 10));
		
		$campo2 = new Zend_Form_Element_Text('gas_name');
		$campo2->setLabel('Nome del gas')
		->setOptions(array(
				"id" => "gas_name",
				"order" => 3,
				"size" => 60,
				"class" => "validate[required]"
		))
		->setRequired(true);
		
		$campo3 = new Application_Form_Element_GasControls("gas_controls");
		$campo3->setOptions(array(
				"id" => "gas_controls",
				"order" => 2
		));
			
		$campo4 = new Zend_Form_Element_Text('gas_source');
		$campo4->setLabel('Fonte')
		->setOptions(array(
				"id" => "gas_source",
				"order" => 4,
				"size" => 60
		));

		$campo5 = new Zend_Form_Element_Text('gas_descr');
		$campo5->setLabel('Descrizione')
		->setOptions(array(
				"id" => "gas_descr",
				"order" => 5,
				"size" => 60
		));

		$campo6 = new Zend_Form_Element_Text('gas_category');
		$campo6->setLabel('Categoria')
		->setOptions(array(
				"id" => "gas_category",
				"order" => 6,
				"size" => 60
		));

		$campo7 = new Zend_Form_Element_Text('gas_thickness');
		$campo7->setLabel('Spessore (mm)')
		->setOptions(array(
				"id" => "gas_thickness",
				"order" => 10,
				"size" => 60,
				"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));
		
		$campo8 = new Zend_Form_Element_Hidden('gas_category_id');
		$campo8->setOptions(array(
				"id" => "gas_category_id",
		));


		// Adding form elements to form
		$this->_form->getSubForm("detail")->addElement($campo1);
		$this->_form->getSubForm("detail")->addElement($campo3);
		$this->_form->getSubForm("detail")->addElement($campo2);
		$this->_form->getSubForm("detail")->addElement($campo4);
		$this->_form->getSubForm("detail")->addElement($campo5);
		$this->_form->getSubForm("detail")->addElement($campo6);
		$this->_form->getSubForm("detail")->addElement($campo7);
		$this->_form->getSubForm("detail")->addElement($campo8);
		
		
		// Elementi hidden non devono avere decorators
		$elements = $this->_form->getSubForm("detail")->getElements();
		foreach ($elements as $elem) {
			if ($elem instanceof Zend_Form_Element_Hidden)
				$elem->setDecorators(array('ViewHelper'));
		}
		
		// Datasources
		Zend_Loader::loadClass('GasCategories');
		$categorie = new GasCategories();
		$recordSet = $categorie->select()->from($categorie, array("id", "name"))
		->where("status > ?", 0)
		->order("name");
		$this->_view->gas_categories = $categorie->fetchAll($recordSet)->toArray();
		
	}
}

